// pages/Goods/Commit/index.js
var common = require("../../../../public.js")
const config = require('../../../../config.js')
const app = getApp();
Page({
    data: {

        userId: '',
        token: '',
        orderId: '',
        xcxOpenid: '',
        isFlag: false,
        cardIdFlag: ''
    },
    wxFn() {
        const that = this
        var tempJson = {
            userId: that.data.userId,
            token: that.data.token,
            orderId: that.data.orderId,
            xcxOpenid: that.data.xcxOpenid,
        }

        var urls = '/wxxcxPayGoodsForChinaUms'
        common.wxPayRequest(urls, tempJson, function (res) {
            console.log(res, "lilili")
            if (res.status !== 200) {
                common.showRequestErrMsg(res)
            } else {
                //微信支付
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    'success': function (res) {
                        common.showToast('支付成功', 'success')

                        that.setData({
                            isFlag: true
                        })

                    },
                    'fail': function (res) {
                        that.setData({
                            isFlag: true
                        })
                        if (res.errMsg === 'requestPayment:fail cancel') { } else {
                            common.showToast(res.errMsg)
                        }
                    }
                })
            }
        }, function () {
            // that.setData({
            //     isFlag:true
            // })

        })
    },

    // 会员卡门票支付
    wxCardPay() {
        let that = this
        let tempJson = {
            userId: that.data.userId,
            token: that.data.token,
            orderId: that.data.orderId,
            xcxOpenid: that.data.xcxOpenid,
        }
        common.wxPayRequest("/wxxcxPayScenicTicketForRegionWLByChinaUms", tempJson, function (res) {
            if (res.status == 200) {
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    'success': function (res) {
                        common.showToast('支付成功', 'success')
                        // wx.navigateTo({
                        //     url: '/pages/ScenicArea/ScenicAreaCommitPage/paySuccess/index?payment='
                        //         + that.data.totalPrice +
                        //         '&orderId=' + orderId +
                        //         '&cardId=' + that.data.cardId,
                        // })
                    },
                    'fail': function (res) {
                        if (res.errMsg === 'requestPayment:fail cancel') {
                            // if (that.data.cardId == '') {//景区
                            //     wx.redirectTo({
                            //         url: '/pages/ScenicArea/ScenicAreaOrder/index?status=1',
                            //     })
                            // } else { //会员卡
                            //     wx.redirectTo({
                            //         url: '/pages/ScenicArea/NianCardOrder/index?status=1',
                            //     })
                            // }

                        } else {
                            common.showToast(res.errMsg)
                        }
                    }
                })
            } else {
                common.showToast(res.msg)
            }

        })

    },

    onLoad: function (options) {
        const that = this
        that.setData({
            orderId: options.orderId ? options.orderId : '4176',
            userId: options.userId ? options.userId : '',
            token: options.token ? options.token : '',
            xcxOpenid: options.xcxOpenid ? options.xcxOpenid : '',
            cardIdFlag: options.cardIdFlag ? options.cardIdFlag : ''
        }, function () {
            if (that.data.token) {
                if (that.data.cardIdFlag==''){   //普通商品支付
                    that.wxFn()  //普通商品支付拉起
                }else if(that.data.cardIdFlag==0){
                    that.wxCardPay()    //会员卡支付
                } else if(that.data.cardIdFlag==1){
                    that.wxFn()  //会员卡商品支付
                }
               
                // that.wxCardPay() 门票支付

            }

        })
    },

})